package com.antball.aaa.command_responsibilitychain_design;

import com.antball.aaa.command_responsibilitychain_design.model.CommandEnum;
import com.antball.aaa.command_responsibilitychain_design.model.CommandVo;

/**
 * @Auther: huangsj
 * @Date: 2019/8/7 10:49
 * @Description:
 */
public class Invoker {

    Command command = null;

    public String exec(String commandStr) {
        String result = "";

        CommandVo commandVo = new CommandVo(commandStr);

        if (CommandEnum.getNames().contains(commandVo.getCommanName())) {

            String className = CommandEnum.valueOf(commandVo.getCommanName()).getValue();

            try {
                command = (Command) Class.forName(className).newInstance();
            } catch (Exception e) {
                e.printStackTrace();
            }
            result = command.execute(commandVo);

        } else {
            result = "无法执行命令";
        }
        return result;

    }
}
